<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
	</view>
</template>

<script>
	// import {openComDB} from '../../sql/sqlite/index.js'
	export default {
		data() {
			return {
				title: 'Hello'
			}
		},
		onLoad() {
			this.getNetworkType()
		},
		methods: {
			//获取网络状态
			getNetworkType() {
				let that = this;
				//获取网络信息  
				uni.getNetworkType({
					success: function(res) {
						let type = res.networkType;
						console.log("是否有网络：" + type);
						if (type === 'none') {

							console.log("执行无网络方法！");
							that.getSqlLite();
						} else {
							console.log("执行有网络方法！");
							that.getSqlLite();
						}
					}
				});
			},
			getSqlLite() {

				let that = this;

				//检查数据库是否打开

				var open = plus.sqlite.isOpenDatabase({

					name: 'main',

					path: '_doc/bim-boot.db'

				})

				if (open) {

					that.executefunctionSql();

				} else {

					//开启数据库

					plus.sqlite.openDatabase({

						name: 'main', //数据库名称

						path: '_doc/bim-boot.db', //数据库地址，uniapp推荐以下划线为开头

						success(e) {

							console.log("打开数据库成功！");

							that.executefunctionSql();

						},

						fail(e) {

							console.log("打开数据库失败！");

							that.$tip.error("打开数据库失败！")

						}

					})

				}

			},

			executefunctionSql() {

				plus.sqlite.executeSql({
							name:'main',
							//表格创建或者打开，后面为表格结构
							sql:'create table if not exists userInfo("list" INTEGER PRIMARY KEY AUTOINCREMENT,"id" TEXT,"name" TEXT,"gender" TEXT,"avatar" TEXT)', 
							success(e){
								resolve(e);
								console.log(e)
								console.log("success")
							},
							fail(e){
								console.log(e)
								reject(e);
							}
						})
						plus.sqlite.executeSql({
										name:'main',
										sql:"INSERT INTO userInfo VALUES('001','100','lhw', '男', '我是头像')",
										success(e){
											resolve(e);
										},
										fail(e){
											console.log(e)
											reject(e);
										}
									})

						plus.sqlite.selectSql({
							name: 'main',
							sql: 'select * from userInfo',
							success: function(data){
								console.log('selectSql success: ');
								console.log(JSON.stringify(data).length+"----")
								console.log()
								data.map((item)=>{
									console.log(item)
								})
							},
							fail: function(e){
								console.log('selectSql failed: '+JSON.stringify(e));
							}
						});
					

			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
